package cn.stylefeng.guns.modular.ry600.controller;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.stylefeng.roses.core.base.controller.BaseController;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;

import cn.stylefeng.guns.core.log.LogObjectHolder;
import cn.stylefeng.guns.core.util.ServerResponse;

import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.plugins.Page;
import com.github.pagehelper.PageHelper;

import cn.stylefeng.guns.modular.system.model.Client;
import cn.stylefeng.guns.modular.system.model.Clientcustomer;
import cn.stylefeng.guns.modular.system.model.Erpkind;
import cn.stylefeng.guns.modular.ry600.service.IClientService;
import cn.stylefeng.guns.modular.ry600.service.IClientcustomerService;
import cn.stylefeng.guns.modular.ry600.service.IClientproductService;
import cn.stylefeng.guns.modular.ry600.service.IErpkindService;

/**
 * 连锁客户验证管理控制器
 *
 * @author fengshuonan
 * @Date 2018-12-06 18:35:42
 */
@Controller
@RequestMapping("/client")
public class ClientController extends BaseController {

	// private String PREFIX = "/ry600/client/";
	private String PREFIX = "/ry600/client/";

	@Autowired
	private IClientService clientService;
	@Autowired
	private IClientcustomerService clientcustomerService;
	@Autowired
	private IClientproductService clientproductService;
	@Autowired
	private IErpkindService erpkindService;

	/**
	 * 跳转到连锁客户验证管理首页
	 */
	@RequestMapping("")
	public String index() {
		// return PREFIX + "client.html";
		return PREFIX + "client.html";
	}

	/**
	 * 跳转到添加连锁客户验证管理
	 */
	@RequestMapping("/client_add")
	public String clientAdd() {
		return PREFIX + "client_add.html";
	}

	/**
	 * 跳转到修改连锁客户验证管理
	 */
	@RequestMapping("/client_update/{clientId}")
	public String clientUpdate(@PathVariable Integer clientId, Model model) {
		Client client = clientService.selectById(clientId);
		model.addAttribute("item", client);
		LogObjectHolder.me().set(client);
		return PREFIX + "client_edit.html";
	}

	/**
	 * 获取连锁客户验证管理列表
	 */
	@RequestMapping(value = "/list")
	@ResponseBody
	public Object list(String condition, HttpServletRequest request) {
		Integer page = Integer.valueOf(request.getParameter("page"));
		Integer rows = Integer.valueOf(request.getParameter("rows"));
		PageHelper.startPage(page, rows);
		Object result = clientService.selectList(null);
		return result;
	}

	/**
	 * 新增连锁客户验证管理
	 */
	@RequestMapping(value = "/add")
	@ResponseBody
	public Object add(Client client) {
		Map<String,Object> msg =new HashMap<String,Object>();
		msg.put("clientId", client.getClientId());
		List<Client> clienttemp =(List<Client>) clientService.selectByMsg(msg);
		if(clienttemp.size()==0){
			clientService.insert(client);
			return ServerResponse.createBySuccessMessage("添加成功");//SUCCESS_TIP;
		}else{
			return ServerResponse.createByErrorMessage("客户已存在");
		}
	}

	/**
	 * 删除连锁客户验证管理
	 */
	@RequestMapping(value = "/delete")
	@ResponseBody
	public Object delete(@RequestParam Integer clientId) {
		Client client = clientService.selectById(clientId);
		clientService.deleteById(clientId);
		List<Erpkind> resultErpKind = erpkindService.selectList(null);
		Set<String> dataSource = new HashSet<String>();
		for (int j = 0; j < resultErpKind.size(); j++) {
			if (dataSource.add(resultErpKind.get(j).getDataSource())) {
				clientcustomerService.delClientCus(client.getClientId(),
						resultErpKind.get(j).getDataSource());
			}
		}
		clientproductService.delClientPro(client.getClientId());
		return SUCCESS_TIP;
	}

	/**
	 * 修改连锁客户验证管理
	 */
	@RequestMapping(value = "/update")
	@ResponseBody
	public Object update(Client client) {
		clientService.updateById(client);
		return SUCCESS_TIP;
	}

	/**
	 * 连锁客户验证管理详情
	 */
	@RequestMapping(value = "/detail/{clientId}")
	@ResponseBody
	public Object detail(@PathVariable("clientId") Integer clientId) {
		return clientService.selectById(clientId);
	}

	/**
	 * 连锁客户条件查找
	 */
	@RequestMapping(value = "/selectByMsg")
	@ResponseBody
	public Object selectByMsg(HttpServletRequest request,
			HttpServletResponse response) {
		Integer page = Integer.valueOf(request.getParameter("page"));
		Integer rows = Integer.valueOf(request.getParameter("rows"));
		Map<String, Object> msg = this.setMap(request);
		PageHelper.startPage(page, rows);
		Object result = clientService.selectByMsg(msg);

		return result;

	}

	public Map<String, Object> setMap(HttpServletRequest request) {
		Map<String, Object> msg = new HashMap<String, Object>();
		String ifUse = request.getParameter("ifUse");
		String searchKey = request.getParameter("searchKey");
		String searchWord = request.getParameter("searchWord");
		if (!StringUtils.isBlank(ifUse)) {
			msg.put("ifUse", ifUse.trim());
		}
		if (!StringUtils.isBlank(searchKey) && !StringUtils.isBlank(searchWord)) {
			msg.put(searchKey.trim(), searchWord.trim());
		}
		return msg;
	}

}
